package com.olap.starter.util.tree;

import com.olap.starter.util.tree.TreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WBG
 * @date 2021/11/9 22:08
 * @describe
 */
public class NodeUtil {

    public static TreeNode getTreeNode(Map<Object, Object> nodeMap, String topKey, boolean setIndex) {
        if (nodeMap == null || nodeMap.size() == 0) {
            return null;
        }

        TreeNode parent = null; //顶级
        for (Object s : nodeMap.keySet()) {
            String key = s.toString();
            TreeNode TreeNode = (TreeNode) nodeMap.get(key);
            if (TreeNode.getKey().equals(topKey)) {//顶级
                parent = TreeNode;
                if (setIndex) {
                    parent.setIndex("0");
                }
            } else {
                TreeNode parents = (TreeNode) nodeMap.get(TreeNode.getParentKey());//获取父级
                if (parents != null) {
                    parents.addChildren(TreeNode);//父级添加子级
                }
            }
        }
        if (parent != null && setIndex) {
            setIndex(parent);
        }
        return parent;
    }
    public static List<TreeNode> getTreeNodeList(Map<Object, Object> nodeMap, String topKey) {
        if (nodeMap == null || nodeMap.size() == 0) {
            return null;
        }
        List<TreeNode> list = new ArrayList<>();
        for (Object s : nodeMap.keySet()) {
            String key = s.toString();
            TreeNode TreeNode = (TreeNode) nodeMap.get(key);
            if (TreeNode.getParentKey().equals(topKey)) {//顶级
                list.add(TreeNode);
            } else {
                TreeNode parents = (TreeNode) nodeMap.get(TreeNode.getParentKey());//获取父级
                if (parents != null) {
                    parents.addChildren(TreeNode);//父级添加子级
                }
            }
        }
        for (int i = 0; i < list.size(); i++) {
            TreeNode parent = list.get(i);
            parent.setIndex(String.valueOf(i));
            setIndex(parent);
        }
        return list;
    }

    public static void setIndex(TreeNode parent) {
        List<TreeNode> childrenList = parent.getChildren();
        if (childrenList == null) {
            return;
        }
        for (int i = 0; i < childrenList.size(); i++) {
            TreeNode children = childrenList.get(i);
            String parIndex = parent.getIndex() == null ? i + "" : parent.getIndex() + "," + i;
            children.setIndex(parIndex);
            setIndex(children);
        }
    }

    public static Map<String, String> getIndex(TreeNode parent, String key) {
        List<TreeNode> childrenList = parent.getChildren();
        Map<String, String> indexMap = new HashMap<>();
        String[] keys = key.split(",");
        for (String k : keys) {
            doIndex(childrenList, indexMap,k);
        }
        return indexMap;
    }

    static void doIndex(List<TreeNode> childrenList,Map<String, String> indexMap, String key) {
        if (childrenList == null) {
            return ;
        }
        for (TreeNode TreeNode : childrenList) {
            if (TreeNode.getKey().equals(key)) {
                indexMap.put(key,TreeNode.getIndex());
            }
            doIndex(TreeNode.getChildren(),indexMap,key);
        }
    }

    public static Map<String, TreeNode> getNodeByKey(TreeNode parent, String key) {
        List<TreeNode> childrenList = parent.getChildren();
        Map<String, TreeNode> indexMap = new HashMap<>();
        String[] keys = key.split(",");
        for (String k : keys) {
            doNodeByKey(childrenList, indexMap,k);
        }
        return indexMap;
    }

    static void doNodeByKey(List<TreeNode> childrenList,Map<String, TreeNode> indexMap, String key) {
        if (childrenList == null) {
            return ;
        }
        for (TreeNode TreeNode : childrenList) {
            if (TreeNode.getKey().equals(key)) {
                indexMap.put(key,TreeNode);
                return;
            }
            doNodeByKey(TreeNode.getChildren(),indexMap,key);
        }
    }
}
